<?php
/**
 * Nweb Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.framework.application.model
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     3.0 $Id$
 * @link        http://code.google.com/p/nweb-framework/
 * @link        http://framework.nweb.pl
 */

namespace pl\nweb\framework\application\model\gateway;

/**
 * Tabela
 *
 * @category    source
 * @package     pl.nweb.framework.application.model
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     3.0 $Id$
 */
class Table
{
    /**
     * Nazwa tabeli
     *
     * @var null|string
     */
    protected $_name = null;

    /**
     * Pola tabeli
     *
     * @var null|array
     */
    private $_fields = array();

    /**
     * Klucz(e) podstawowe
     *
     * @var null|string|array
     */
    private $_primary = null;

    /**
     * Adapter połączenia z bazą danych
     *
     * @var null|\pl\nweb\framework\db\Adapter
     */
    private $_adapter = null;

    /**
     * Nazwa adaptera (w rejestrze)
     *
     * @var string
     */
    private $_adapterName = \pl\nweb\framework\db\adapter\Registry::NAME_DEFAULT;

    /**
     * Opcje
     */
    const OPTION_NAME         = 'name';
    const OPTION_FIELDS       = 'fields';
    const OPTION_PRIMARY      = 'primary';
    const OPTION_ADAPTER      = 'adapter';
    const OPTION_ADAPTER_NAME = 'adapter.name';

    /**
     * Konstruktor
     *
     * @param  array $options opcje
     */
    public function __construct (array $options = array())
    {
        if(isset($options[self::OPTION_NAME])) {
            $this->_name = $options[self::OPTION_NAME];
        }

        if(isset($options[self::OPTION_FIELDS])) {
            $this->_fields = $options[self::OPTION_FIELDS];
        }

        if(isset($options[self::OPTION_PRIMARY])) {
            $this->_primary = $options[self::OPTION_PRIMARY];
        }

        if(isset($options[self::OPTION_ADAPTER])) {
            $this->setAdapter($options[self::OPTION_ADAPTER]);
        }

        if(isset($options[self::OPTION_ADAPTER_NAME])) {
            $this->_adapterName = $options[self::OPTION_ADAPTER_NAME];
        }
    }

    /**
     * Definiuje adapter połączenia z bazą danych
     *
     * @param \pl\nweb\framework\db\Adapter $adapter adapter połączenia
     * @return void
     */
    public function setAdapter (\pl\nweb\framework\db\Adapter $adapter)
    {
        $this->_adapter = $adapter;
    }

    /**
     * Zwraca adapter połączenia z bazą danych
     *
     * @return \pl\nweb\framework\db\Adapter
     * @throws \pl\nweb\framework\application\model\gateway\table\Exception
     */
    public function getAdapter ()
    {
        if(null === $this->_adapter) {
            if(\pl\nweb\framework\db\adapter\Registry::has($this->_adapterName)) {
                $this->setAdapter(\pl\nweb\framework\db\adapter\Registry::get($this->_adapterName));
            } else {
                throw new table\Exception ('No adapter found', 121001);
            }
        }
        return $this->_adapter;
    }
}